package test.android.http;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;



public class HTTPActivity extends Activity
{
    private TableLayout table;
    private TableRow.LayoutParams rowParams;
    private String buf;
    private DataReceiver dataReceiver;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
        ReceiverTask task = new ReceiverTask();
        task.execute();
        //getData();                  
    }    
    
    private void renew() {
        
    }

    private void initView() {
        table = (TableLayout)findViewById(R.id.tablelayout01);
        rowParams = new TableRow.LayoutParams();
        rowParams.weight = 1;
        rowParams.width = LayoutParams.FILL_PARENT;
        rowParams.height = LayoutParams.WRAP_CONTENT;
    }
   
    private void createRow(String buf){
        TableRow row = new TableRow(this);
        row.setGravity(Gravity.CENTER);
        table.addView(row);
        TextView label = new TextView(this);
        label.setLayoutParams(rowParams);
        label.setGravity(Gravity.LEFT);
        label.setText(buf);
        row.addView(label);
    }
    
    private class ReceiverTask extends AsyncTask<Void, String, Void> {

        @Override
        protected void onPreExecute(){
            table.removeAllViews();
        } 
        
        @Override
        protected Void doInBackground(Void... args) {
            dataReceiver = new DataReceiver();
            if (dataReceiver.httpConnect("http://192.168.0.51/test/android-http-inputform/getsales.php")==false)
                if(dataReceiver.httpConnect("http://213.33.134.22/test/android-http-inputform/getsales.php")==false){
                    publishProgress(getString(R.string.fail));
                    return null;
                }                        
            while ((buf=dataReceiver.nextRow())!=null){
                publishProgress(buf);
            }            
            return null;
        }
        
        @Override
        protected void onProgressUpdate(String... args){
            createRow(args[0]);
        } 
    
    };

}

